package com.mfjhss.springcloud.controller;

import com.mfjhss.springcloud.entity.CommonResult;
import com.mfjhss.springcloud.entity.Payment;
import com.mfjhss.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * (Payment)表控制层
 *
 * @author mfjhss
 * @since 2021-07-26 15:14:26
 */
@RestController
@Slf4j
@RequestMapping("payment")
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

    /**
     * 服务对象
     */
    @Resource
    private PaymentService paymentService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Payment selectOne(@PathVariable("id") Long id) {
        return this.paymentService.queryById(id);
    }

    @PostMapping(value = "/create")
    public CommonResult create(@RequestBody Payment payment){
        log.info(payment.toString());
        int result = paymentService.insert(payment);
        log.info("***插入结果:"+ result);

        if(result > 0){
            return new CommonResult(200,"插入数据成功"+serverPort,result);
        }else {
            return new CommonResult(444,"插入数据失败"+serverPort,null);
        }
    }

    @GetMapping(value = "/get/{id}")
    public CommonResult getPayment(@PathVariable("id") Long id){
        log.info("查询id："+id);
        Payment payment = paymentService.queryById(id);
        return new CommonResult(200,"查询成功",payment);
    }

    @GetMapping(value = "/feign/timeout")
    public String paymentFeignTimeOut(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }

    @GetMapping("/lb")
    public String getPaymentLB(){
        return serverPort;
    }

    @GetMapping("/zipkin")
    public String paymentZipkin(){
        return "This is payment.zipkin";
    }

}
